<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*anke 第二版接口 api*/
class AkV2Api extends  BaseApi
{
	const GetPTEmployeeList="http://host.gymparty.net:81/CSmart/SCHWebService.asmx/GetPTEmployeeList";// 私教列表
	const GetReservationList="http://host.gymparty.net:81/CSmart/SCHWebService.asmx/GetReservationList";//预约列表
	const DoLessonStart="http://host.gymparty.net:81/CSmart/SCHWebService.asmx/DoLessonStart"; //上课
	const DoLessonEnd="http://host.gymparty.net:81/CSmart/SCHWebService.asmx/DoLessonEnd"; //下课
	protected $BrandKey;//品牌id
	protected $ClubId;//健身房id
	protected $Key;//健身房key
	
	
	
	/**
	 * 测试地址
	 * http://anke.com/api/AkV2Api/GetPTEmployeeList/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c/shop_id/20
	 * */
	 public function _initialize()
    {
//  	parent::_initialize();
//		if(empty(input("shop_id"))){
//			return $this->ajax_error('',"0003",FALSE);
//			die;
//		}
    	if(empty(cache("AnkeBrandKey"))){
			 $config=config("database.anke_config");
			 $this->BrandCode=$config['brand_code'];
			 $this->ClubId=$config['club_id'];
			 $this->Key=$config['key'];
			 cache("AnkeBrandKey",$config['brand_code']);
			 cache("AnkeClubId",$config['club_id']);
			 cache("AnkeKey",$config['key']);   		
    	}else{
    		$this->BrandCode=cache("AnkeBrandKey");
			$this->ClubId=cache("AnkeClubId");
			$this->Key=cache("AnkeKey");
    	}
    }
	/***
	 * 获取店铺名称
	 **/
	public function GetShopName(){
			return $this->ajax_success(array("shop_name"=>config("database.shop_name")));
	}
	/**
	 * 私教获取
	 * http://anke.com/api/AkV2Api/GetPTEmployeeList/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c/shop_id/20
	 * */
	public  function GetPTEmployeeList(){
		$res= post_submit_xml(self::GetPTEmployeeList,array("BrandCode"=>$this->BrandCode,"ClubId"=>$this->ClubId,"Key"=>$this->Key));
		if(empty($res)){
			return $this->ajax_error('','0003','安科配置错误或者私教列表为空');
		}else{
			return $this->ajax_success(json_decode($res[0],TRUE));
		}
	}
	
	/**
	 * 获取预约课程列表
	 * http://anke.com/api/AkV2Api/GetReservationList/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c/shop_id/20/EmployeeID/AE833CA4-16C9-4B5C-B666-39BD4AE7413C/EmployeeCardNo/0723109292
	 * */
	 public function GetReservationList(){
	 	if(empty(input("EmployeeCardNo"))||empty(input("EmployeeID"))){
	 		return $this->ajax_error("","0003","EmployeeCardNo,EmployeeID 为空");
	 	}
		$count=db("Interface")->where("owner_type=2 and card_no='".input("EmployeeCardNo")."'")->count(1);
		if($count<1){
			return $this->ajax_error("","0003","pt 在中未智联系统不存在");
		}
	 	$res= post_submit_xml(self::GetReservationList,array("BrandCode"=>$this->BrandCode,"ClubId"=>$this->ClubId,"Key"=>$this->Key,"EmployeeID"=>input("EmployeeID")));
		if(empty($res)){
			return $this->ajax_error('','0003','安科配置错误或者约课列表为空');
		}else{
			return $this->ajax_success(json_decode($res[0],TRUE));
		}
	 }
	 /**
	  * 获取上课教练的指纹和上课会员的指纹
	  * http://anke.com/api/AkV2Api/GetFingerPTAndMember/shop_id/20/EmployeeCardNo/0723109292/MemberID/58ade9ed-afa1-4b0d-8a05-53999960ef95
	  */
	 public function GetFingerPTAndMember(){
	 	if(empty(input("EmployeeCardNo")||empty(input("MemberID")))){
	 		return $this->ajax_error("","0003","MemberID,EmployeeCardNo 为空");
	 	}else{
	 		if(input("varify_type")==2){//获取人脸模板
	 			$pt_finger=db("FaceCode")->where("card_no='".input("EmployeeCardNo")."'")->value("face1");
				if(empty($pt_finger)){
					return $this->ajax_error("","0003","上课教练人脸模板为空！");
				}
				$member_finger=db("FaceCode")->where(" card_no= (select card_no from m_interface where member_id='".input("MemberID")."')")->value("face1");
				if(empty($member_finger)){
					return $this->ajax_error("","0003","上课学员人脸模板为空！");
				}
	 		}else{//默认是指纹模板
	 			$pt_finger=db("Interface")->where("card_no='".input("EmployeeCardNo")."'")->value("fingerprint");
				if(empty($pt_finger)){
					return $this->ajax_error("","0003","上课教练指纹为空！");
				}
				$member_finger=db("Interface")->where("member_id='".input("MemberID")."'")->value("fingerprint");
				if(empty($member_finger)){
					return $this->ajax_error("","0003","上课学员指纹为空！");
				}	
	 		}
			return $this->ajax_success(array("PTFinger"=>$pt_finger,"MemberFinger"=>$member_finger,"varify_type"=>input("varify_type")));
	 	}
	 }
	
	/**
	 * 开始上课
	 * http://anke.com/api/AkV2Api/DoLessonStart/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c/shop_id/20/EmployeeID/AE833CA4-16C9-4B5C-B666-39BD4AE7413C/MemberID/fe71c953-515e-4e83-825b-38a657ed0c4d/ReservationID/9f4d13c3-3eba-4028-96ce-3cb86cb98c45
	 * */
	 public function DoLessonStart(){
	 	if(empty(input("EmployeeID"))||empty(input("MemberID"))||empty(input("ReservationID"))){
	 		return $this->ajax_error("","0003","EmployeeID,MemberID,ReservationID为空");
	 	}
	 	$res= post_submit_xml(self::DoLessonStart,array("BrandCode"=>$this->BrandCode,"ClubId"=>$this->ClubId,"Key"=>$this->Key,"EmployeeID"=>input("EmployeeID"),"MemberID"=>input("MemberID"),"ReservationID"=>input("ReservationID")));
		if(empty($res)){
			return $this->ajax_error('','0003','安科配置错误或者参数错误');
		}else{
			$res=json_decode($res[0],TRUE);
			if($res["Status"]==1){
				return $this->ajax_success($res["Remark"]);
			}
			return $this->ajax_error('','0003',$res["Remark"]);
		}
	 }
	 /**
	 * 开始下课
	 * http://anke.com/api/AkV2Api/DoLessonEnd/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c/shop_id/20/EmployeeID/AE833CA4-16C9-4B5C-B666-39BD4AE7413C/MemberID/fe71c953-515e-4e83-825b-38a657ed0c4d/ReservationID/9f4d13c3-3eba-4028-96ce-3cb86cb98c45/Number/1
	 * */
	 public function DoLessonEnd(){	
	 	if(empty(input("EmployeeID"))||empty(input("MemberID"))||empty(input("ReservationID")||empty(input("Number")))){
	 		return $this->ajax_error("","0003","EmployeeID,MemberID,ReservationID,Number为空");
	 	}
	 	$res= post_submit_xml(self::DoLessonEnd,array("BrandCode"=>$this->BrandCode,"ClubId"=>$this->ClubId,"Key"=>$this->Key,"EmployeeID"=>input("EmployeeID"),"MemberID"=>input("MemberID"),"ReservationID"=>input("ReservationID"),"Number"=>input("Number")));
		if(empty($res)){
			return $this->ajax_error('','0003','安科配置错误或者参数错误');
		}else{
			$res=json_decode($res[0],TRUE);
			if($res["Status"]==1){
				return $this->ajax_success($res["Remark"]);
			}
			return $this->ajax_error('','0003',$res["Remark"]);
		}
	 }
	 //图片上传
	 public function UploadImage(){
	 	 $file = request()->file('file');
	 	 if(!empty($file)){
	 	 	 $info = $file->move(ROOT_PATH . 'data' . DS . 'attendance');
			 return $this->ajax_success();
	 	 }else{
	 	 	return $this->ajax_error('','0003',"图片为空！");
	 	 }
	 }
	 //测试图片上传
	 public function test_upload(){
	  	$client = new \GuzzleHttp\Client();
		$response=$client->request('POST',"http://127.0.0.1/api/AkV2Api/UploadImage", [
			'multipart' => [
	        [
	            'name'     => 'file',
	            'contents' => fopen("./data/face/test.jpg", 'r')
	        ]
	        
		]]);
		echo $response->getBody();
	}
	 
}
